<%--
  Created by IntelliJ IDEA.
  User: zhangdeng
  Date: 2020/12/02
  Time: 22:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.User"%>
<%@ page import="com.sjsq.dao.UserDao"%>
<%@ page import="com.sjsq.dao.impl.UserDaoImpl"%>
<%@ page import="com.sjsq.service.UserService" %>
<%@ page import="com.sjsq.service.impl.UserServiceImpl" %>
<%@ page import="java.util.List" %>
<%
        // 获取绝对路径路径 ,开发项目一定要使用绝对路径，不然肯定出错
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":"
                + request.getServerPort() + path + "/";
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath %>" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户登录</title>
    <style type="text/css">
        h1{
            text-align: center;
        }
        h4{
            text-align: center;color: red;
        }
        body{
            background-color: antiquewhite;
        }
    </style>
</head>
<body>
    <h1>现存图书列表</h1>
    <hr>
    <h4>---装饰中---</h4>
    <%
        // 设置接收的编码为UTF-8
        request.setCharacterEncoding("utf-8");
        User user = new User();
        UserDao dao = new UserDaoImpl();
        String name = request.getParameter("name");
        String password=request.getParameter("password");

        user.setName(name);
        user.setPassword(password);

        User us=dao.login(user);

        // 把数据库里面的User获取出来
        UserService service = new UserServiceImpl();
        List<User> list = service.selectUser(user);
        for(int i=0;i<list.size();i++){
            user = list.get(i);
        }


        System.out.println("显示用户信息：");
        System.out.println(user);

        System.out.println("----us的信息----");
        System.out.println(us);

        session.setAttribute("user",user);

        // 原来的代码
//        if(us != null){
//            response.sendRedirect("book.jsp");
//        }else{
//            response.sendRedirect("fail.jsp");
//        }

        // 1是管理者，0是学生，此处的isadmin必须填写不能为空
        if(us.getIsadmin().equals(1)){
            response.sendRedirect("admin.jsp");
        }
        else if(!us.getIsadmin().equals(1)){
            response.sendRedirect("book.jsp");
        }
        else{
            response.sendRedirect("fail.jsp");
        }
    %>
</body>
</html>
